<?php
/**
 * Barcode class
 *
 * @package		My own library
 * @subpackage	Libraries
 * @category	Barcode
 * @author		Tatang Al-Mubarak
 * @link		http://its-codeme.googlecode.com/
 */
 
class Barcode {

	private $_data = '';
	private $_use_star = FALSE;
	
	public function __construct()
	{}
	
	/**
	 * Barcode Set Data
	 *
	 * This is a function used for set barcode data
	 *
	 * @access	public
	 * @return	void
	 */
	public function set_data($data)
	{
		$this->_data = $data;
	}
	
	/**
	 * Barcode Show
	 *
	 * This is a function used for show barcode
	 *
	 * @access	public
	 * @return	bool
	 */
	public function show()
	{
		return $this->_create();
	}
	
	/**
	 * Barcode Create Bar
	 *
	 * This is a function used for create black & white bar
	 *
	 * @access	private
	 * @return	bool
	 */
	private function _create_bar($im, $i, $x, $y, $l, $t, $black, $white)
	{
		// $i urutan, $x posisi x (kiri atas), $y posisi y (kanan bawah),
		// $l = Lebar bar
		if ($i % 2 == 0) { // genap == HITAM
			imagefilledrectangle($im, $x, $y, $x + $l, $y + ($t), $black);
		} else { 	// ganjil == PUTIH
			imagefilledrectangle($im, $x, $y, $x + $l, $y + ($t), $white);
		}
	}
	
	/**
	 * Barcode Create
	 *
	 * This is a function used for create barcode
	 *
	 * @access	private
	 * @return	bool
	 */
	private function _create()
	{
		header('Content-type: image/png');
		
		$bar["0"]="000110100";
		$bar["1"]="100100001";
		$bar["2"]="001100001";
		$bar["3"]="101100000";
		$bar["4"]="000110001";
		$bar["5"]="100110000";
		$bar["6"]="001110000";
		$bar["7"]="000100101";
		$bar["8"]="100100100";
		$bar["9"]="001100100";
		$bar["*"]="010010100";
		$bar["A"]="100001001";
		$bar["B"]="001001001";
		$bar["C"]="101001000";
		$bar["D"]="000011001";
		$bar["E"]="100011000";
		$bar["F"]="001011000";
		$bar["G"]="000001101";
		$bar["H"]="100001100";
		$bar["I"]="001001100";
		$bar["J"]="000011100";
		$bar["K"]="100000011";
		$bar["L"]="001000011";
		$bar["M"]="101000010";
		$bar["N"]="000010011";
		$bar["O"]="100010010";
		$bar["P"]="001010010";
		$bar["Q"]="000000111";
		$bar["R"]="100000110";
		$bar["S"]="001000110";
		$bar["T"]="000010110";
		$bar["U"]="110000001";
		$bar["V"]="011000001";
		$bar["W"]="111000000";
		$bar["X"]="010010001";
		$bar["Y"]="110010000";
		$bar["Z"]="011010000";
		$bar["-"]="010000101";
		$bar["."]="110000100";
		$bar[" "]="011000100";
		$bar["$"]="010101000";
		$bar["/"]="010100010";
		$bar["+"]="010001010";
		$bar["%"]="000101010";

		$data = strtoupper(trim($this->_data));
		
		if ($this->_use_star) {
			$data = str_replace("*", ",", $data);
			$data = "*". $data . "*";
		}
		
		$count = strlen($data);
		$ja = 4;


		$pt = 53;
		$l = 2; // size barcode image
		$rasio = 2.5;
		$width = (($count) * ((3 * $rasio) + 6) * $l) + ((($count) + 1) * $l) + (8 * $l); 
		 
		//$im = imagecreate($lebar,($l*$pt)+20+ (4*$l).str_replace("/"));
		$im = imagecreate($width, ($l * $pt) + 20 + (8 * $l));
			
		$white = imagecolorallocate($im, 255, 255, 255);
		$black = imagecolorallocate($im, 0, 0, 0);
		
		$x0 = 4 * $l;
		$akhir = $x = 4 * $l;
		$y = 4 * $l;
		$awal = 0;
		for ($i = 0; $i < $count; $i++) {
			if ($bar[$data[$i]] == "") {
				$data[$i] = " ";
			}
			$count2 = strlen($bar[$data[$i]]);
		
			$awal = $akhir;
			for ($j = 0; $j < $count2; $j++) {
		
				if ($bar[$data[$i]][$j] == "1") {
					$this->_create_bar($im, $j, $x, $y, $l * $rasio, $l * $pt, $black, $white);
					$x += $l * $rasio;
				} else {
					$this->_create_bar($im, $j, $x, $y, $l, $l * $pt, $black, $white);
					$x += $l;
				}
				
			}
			$this->_create_bar($im, 1, $x, $y, $l, $l * $pt, $black, $white);
			$x += $l;
			$akhir = $x;
			//imagestring ($im, 2, ($awal + $akhir) / 2, $y + ($l * $pt) + 5, $data[$i], $black);
			imagestring($im, 4, ($awal + $akhir) / 2, $y + ($l * $pt) + 5, $data[$i], $black);
		}

		imagepng($im);
		imagedestroy($im);
	}
}